package com.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.entity.Doctor;
import com.health.entity.pojo.PageData;
import com.health.entity.pojo.dto.DoctorDTO;
import com.health.entity.pojo.vo.ConsultingroomVO;
import com.health.entity.pojo.vo.DoctorVO;
import com.health.mapper.DoctorMapper;
import com.health.service.IDoctorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cl
 * @since 2023-04-15
 */
@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements IDoctorService {
    @Autowired
    DoctorMapper doctorMapper;

    private LambdaQueryWrapper<Doctor> wrapper ;

    @Override
    //[分页]根据条件-[展示(查询)]医生
    public PageData listDoctorByPageAndCondition(Integer pageNum, Integer pageSize, DoctorDTO dto) {
        wrapper = new LambdaQueryWrapper<>();
        //条件[查询]时，通过输入的医院编号hospital_id的值，查询出对应医院的name名,用于模糊查询的第2个条件
//        String hospitalName = doctorMapper.getHospitalNameById(dto.getHospitalId());
//        Integer hospitalId = doctorMapper.getHospitalIdByLikeName();
//        if (dto!=null){
//            wrapper.like( !StringUtils.isEmpty( dto.getDoctorName() ), Doctor::getName, dto.getDoctorName() )
//                    .like( dto.getHospitalLikeName() != null, Hospital::getName, hospitalName )
//            ;
//        }
        IPage<DoctorVO> pageBase = new Page<>(pageNum, pageSize);

        Page<DoctorVO> doctorsByConditions = doctorMapper.getDoctorByConditions(pageBase, dto);
        PageData pgData = new PageData( doctorsByConditions.getTotal(), doctorsByConditions.getRecords() );

//        wrapper.eq("",  doctorByConditions);
//        List<Doctor> doctorByConditions = doctorMapper.getDoctorByConditions(dto);
//        IPage<Doctor> pageBase = new Page<>(pageNum, pageSize);
//        IPage<Doctor> doctorIPage = baseMapper.selectPage(pageBase, wrapper);
//        PageData pgData = new PageData( doctorIPage.getTotal(), doctorIPage.getRecords() );
        return pgData;
    }

    @Override
    //[添加]医生时，为'所属医院'下拉框赋值([展示](查询)出所有医院id、名字
    public List<Doctor> getAllHospitalNameAndId() {
        List<Doctor> hospitalNamesAndId = doctorMapper.getAllHospitalNameAndId();
        return hospitalNamesAndId;
    }

    @Override
    //[添加]医生时，为'所属科室'下拉框(2个)分别赋值([展示](查询)出所有一级科室名称、二级科室名称)
    public List<ConsultingroomVO> getAllConsultingRoom(Long parent_id) {
//    public List<ConsultingroomVO> getAllConsultingRoom() {
        //先查询出一级科室(parent_id == 0)或二级科室(parent_id != 0)的名称
        List<ConsultingroomVO> consultingRooms = doctorMapper.getConsultingroom(parent_id);
        //若(parent_id值0，则表明上步查询的是一级科室，则此处再查询该一级科室内对应的所有二级科室
        if (parent_id == 0L){
            for (ConsultingroomVO consultingRoom : consultingRooms) {
                Long parentId = consultingRoom.getId();
                consultingRoom.setConsSecond( doctorMapper.getConsultingroom(parentId) );
            }
        }
        return consultingRooms;
    }

}
